
// dbDlg.cpp : implementation file
//

#include "stdafx.h"
#include "db.h"
#include "dbDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#ifdef WIN32

static int mount_flag = 1;
int win32_mount(int flag)
{
	mount_flag = flag;
	return mount_flag;
}

int yp3_get_sd_status(int dev)
{
	return mount_flag;
}

#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CdbDlg dialog


CdbDlg::CdbDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CdbDlg::IDD, pParent)
	, m_InputString(_T(""))
	, m_InputOffset(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CdbDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT3, m_InputString);
	DDX_Text(pDX, IDC_EDIT4, m_InputOffset);
}

BEGIN_MESSAGE_MAP(CdbDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE(WM_DB_CALLBACK, &CdbDlg::OnDBCallback)
	ON_BN_CLICKED(IDC_BUTTON1, &CdbDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_RADIO3, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO4, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO5, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO6, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO7, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO8, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO9, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO10, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO11, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO12, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO13, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO14, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO15, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO16, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO17, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO18, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO19, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO20, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO21, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO22, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO23, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO24, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO25, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO26, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO27, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO28, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO29, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO30, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO31, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO32, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO33, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO34, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO35, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO36, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO37, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO38, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO39, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO40, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO41, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO42, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO43, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO44, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO45, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO46, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO47, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO48, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO49, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO50, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO51, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO52, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO53, &CdbDlg::OnBnClickedRadio01)
	ON_BN_CLICKED(IDC_RADIO54, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO55, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO56, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO57, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO58, &CdbDlg::OnBnClickedRadio10)
	ON_BN_CLICKED(IDC_RADIO59, &CdbDlg::OnBnClickedRadio11)
	ON_BN_CLICKED(IDC_RADIO60, &CdbDlg::OnBnClickedRadio00)
	ON_BN_CLICKED(IDC_RADIO61, &CdbDlg::OnBnClickedRadio00)
END_MESSAGE_MAP()


// CdbDlg message handlers

void CdbDlg::OnOK()
{
	//CDialogEx::OnOK();
}

void CdbDlg::OnCancel()
{
	db_free();
	CDialogEx::OnCancel();
}

LRESULT CdbDlg::OnDBCallback(WPARAM state, LPARAM code)
{
	CString str;
	CString tmp = _T("");
	static int step = -1;

	if((int)state == -666)
	{
		tmp.Format(_T("db ready! state = %d \r\n"), state);
	}
	else if((int)state == -555)
	{
		if(code != step)
		{
			step = code;
			tmp.Format(_T("db init step = %d, tick=\t%d \r\n"), step, ::GetTickCount());
		}
	}
	else if((int)state == -10)
	{
		tmp.Format(_T("scan folder enter=%s \r\n"), CString((char*)code).GetBuffer());
	}
	else if((int)state == -11)
	{
		tmp.Format(_T("scan file No.=%d \r\n"), (int)code);
	}
	else if((int)state == -12)
	{
		tmp.Format(_T("scan folder leave=%s \r\n"), CString((char*)code).GetBuffer());
	}
	else if((int)state < 5)
	{
		tmp.Format(_T("state=%d, code=%d, tick=\t%d \r\n"), state, code, ::GetTickCount());
	}
	else
	{
		tmp.Format(_T("line=%d, file=%s \r\n"), state, CString((char*)code).GetBuffer());
	}

	int cnt = GetDlgItemText(IDC_EDIT2, str);
	if(cnt > 1024*10)
	{
		str = tmp;
	}
	else
	{
		str += tmp;
	}

	SetDlgItemText(IDC_EDIT2, str);
	UpdateData();
	return 1;
}

void DBCallback(int state, int code)
{
	CWnd * pWnd = ::AfxGetApp()->GetMainWnd();
	pWnd->PostMessage(WM_DB_CALLBACK, (WPARAM)state, (LPARAM)code);
	::Sleep(1);
}

BOOL CdbDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	CheckRadioButton(IDC_RADIO1, IDC_RADIO2, mount_flag ? IDC_RADIO1 : IDC_RADIO2);
	CheckRadioButton(IDC_RADIO3, IDC_RADIO61, IDC_RADIO3);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CdbDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CdbDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CdbDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CdbDlg::OnBnClickedRadio00()
{
	GetDlgItem(IDC_EDIT3)->EnableWindow(0); SetDlgItemText(IDC_EDIT3, _T(""));
	GetDlgItem(IDC_EDIT4)->EnableWindow(0); SetDlgItemText(IDC_EDIT4, _T("0"));
}

void CdbDlg::OnBnClickedRadio01()
{
	GetDlgItem(IDC_EDIT3)->EnableWindow(0); SetDlgItemText(IDC_EDIT3, _T(""));
	GetDlgItem(IDC_EDIT4)->EnableWindow(1); SetDlgItemText(IDC_EDIT4, _T("0"));
}

void CdbDlg::OnBnClickedRadio10()
{
	GetDlgItem(IDC_EDIT3)->EnableWindow(1); SetDlgItemText(IDC_EDIT3, _T("unknow"));
	GetDlgItem(IDC_EDIT4)->EnableWindow(0); SetDlgItemText(IDC_EDIT4, _T("0"));
}

void CdbDlg::OnBnClickedRadio11()
{
	GetDlgItem(IDC_EDIT3)->EnableWindow(1); SetDlgItemText(IDC_EDIT3, _T("unknow"));
	GetDlgItem(IDC_EDIT4)->EnableWindow(1); SetDlgItemText(IDC_EDIT4, _T("0"));
}

void CdbDlg::OnBnClickedButton1()
{
	int res;
	CString str;
	AUDIORECORD audio_rec[20];
	VIDEORECORD video_rec[20];
	EBOOKRECORD ebook_rec[20];
	RECUTF8NAME utf8name[20];
	RECID3ITEM id3item[20];
	FOLDERRECORD folder_rec[20];
	TAGRECORD tag_rec[1];

	CWaitCursor wc;

	UpdateData();

	switch(GetCheckedRadioButton(IDC_RADIO3, IDC_RADIO61))
	{
	case IDC_RADIO3 :
		res = db_init(::DBCallback, yp3_get_sd_status,FF_CODE_PAGE);
		if(res)
			SetDlgItemText(IDC_EDIT1, _T("db_init error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_init OK\r\n\r\n"));
		break;

	case IDC_RADIO4 :
		db_free();
		SetDlgItemText(IDC_EDIT1, _T("db_free OK\r\n\r\n"));
		break;

	case IDC_RADIO5 :
		win32_mount(1);
		CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO1);
		res = db_mount();
		if(res)
			SetDlgItemText(IDC_EDIT1, _T("db_mount error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_mount OK\r\n\r\n"));
		break;

	case IDC_RADIO6 :
		win32_mount(0);
		CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO2);
		res = db_unmount();
		if(res)
			SetDlgItemText(IDC_EDIT1, _T("db_unmount error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_unmount OK\r\n\r\n"));
		break;

	case IDC_RADIO7 :
		res = db_is_dirty(MODULE_ALL);
		str.Format(_T("db_is_dirty = %#08x\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO8 :
		res = db_audio_build(0);
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_audio_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_audio_build OK\r\n\r\n"));
		break;

	case IDC_RADIO9:
		res = db_get_audio_count();
		str.Format(_T("db_get_audio_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO10:
		res = db_get_album_count();
		str.Format(_T("db_get_album_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO11:
		res = db_get_artist_count();
		str.Format(_T("db_get_artist_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO12:
		res = db_get_audio_records(m_InputOffset, audio_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_records error\r\n\r\n"));
		else
			ShowAudioRecords(audio_rec, res);
		break;

	case IDC_RADIO13:
		res = db_get_audio_name(m_InputOffset, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_records error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO14:
		res = db_get_albums(m_InputOffset, id3item, 20);
		if (res == -1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_albums error\r\n\r\n"));
		else
			ShowID3Item(id3item, res);
		break;

	case IDC_RADIO15:
		res = db_get_artists(m_InputOffset, id3item, 20);
		if (res == -1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_artists error\r\n\r\n"));
		else
			ShowID3Item(id3item, res);
		break;

	case IDC_RADIO16:
		res = db_get_audio_count_of_album(WideCharToUtf8(m_InputString).GetBuffer(MAXID3ITEM));
		str.Format(_T("db_get_audio_count_of_album = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO17:
		res = db_get_audio_count_of_artist(WideCharToUtf8(m_InputString).GetBuffer(MAXID3ITEM));
		str.Format(_T("db_get_audio_count_of_artist = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO18:
		res = db_get_audio_name_by_album(WideCharToUtf8(m_InputString).GetBuffer(MAXID3ITEM), m_InputOffset, utf8name, 20);
		if (res == -1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_name_by_album error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO19:
		res = db_get_audio_name_by_artist(WideCharToUtf8(m_InputString).GetBuffer(MAXID3ITEM), m_InputOffset, utf8name, 20);
		if (res == -1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_name_by_artist error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO20:
		res = db_get_audio_record_by_album(WideCharToUtf8(m_InputString).GetBuffer(MAXID3ITEM), m_InputOffset, audio_rec, 20);
		if (res == -1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_record_by_album error\r\n\r\n"));
		else
			ShowAudioRecords(audio_rec, res);
		break;

	case IDC_RADIO21:
		res = db_get_audio_record_by_artist(WideCharToUtf8(m_InputString).GetBuffer(MAXID3ITEM), m_InputOffset, audio_rec, 20);
		if (res == -1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_record_by_artist error\r\n\r\n"));
		else
			ShowAudioRecords(audio_rec, res);
		break;

	case IDC_RADIO22:
		res = db_video_build(0);
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_video_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_video_build OK\r\n\r\n"));
		break;

	case IDC_RADIO23:
		res = db_get_video_count();
		str.Format(_T("db_get_video_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO24:
		res = db_get_video_records(m_InputOffset, video_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_video_records error\r\n\r\n"));
		else
			ShowVideoRecords(video_rec, res);
		break;

	case IDC_RADIO25:
		res = db_get_video_name(m_InputOffset, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_video_name error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO26:
		res = db_image_build(0);
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_image_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_image_build OK\r\n\r\n"));
		break;

	case IDC_RADIO27:
		res = db_get_image_count();
		str.Format(_T("db_get_image_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO28:
		res = db_get_image_records(m_InputOffset, video_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_image_records error\r\n\r\n"));
		else
			ShowVideoRecords(video_rec, res);
		break;

	case IDC_RADIO29:
		res = db_get_image_name(m_InputOffset, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_image_name error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO30:
		res = db_ebook_build(0);
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_ebook_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_ebook_build OK\r\n\r\n"));
		break;

	case IDC_RADIO31:
		res = db_get_ebook_count();
		str.Format(_T("db_get_ebook_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO32:
		res = db_get_ebook_records(m_InputOffset, ebook_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_ebook_records error\r\n\r\n"));
		else
			ShowEBookRecords(ebook_rec, res);
		break;

	case IDC_RADIO33:
		res = db_get_ebook_name(m_InputOffset, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_ebook_name error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO34:
		{
			unsigned int ebookSN;
			res = db_get_ebookSN(m_InputString.GetBuffer(), &ebookSN);
			if (res==-1)
				SetDlgItemText(IDC_EDIT1, _T("db_get_ebook_index error\r\n\r\n"));
			else
			{
				str.Format(_T("db_get_ebook_index = %d\r\n"), ebookSN);
				SetDlgItemText(IDC_EDIT1, str);
			}
		}
		break;

	case IDC_RADIO35:
		res = db_wave_build();
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_wave_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_wave_build OK\r\n\r\n"));
		break;

	case IDC_RADIO36:
		res = db_get_wave_count();
		str.Format(_T("db_get_wave_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO37:
		res = db_get_wave_records(m_InputOffset, video_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_wave_records error\r\n\r\n"));
		else
			ShowVideoRecords(video_rec, res);
		break;

	case IDC_RADIO38:
		res = db_get_wave_name(m_InputOffset, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_wave_name error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO39:
		res = db_add_wave(m_InputString.GetBuffer());
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_add_wave error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_add_wave OK\r\n\r\n"));
		break;

	case IDC_RADIO40:
		res = db_enter_folder(m_InputString.GetBuffer());
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_enter_folder error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_enter_folder OK\r\n\r\n"));
		break;

	case IDC_RADIO41:
		res = db_get_count_of_folder(m_InputString.GetBuffer());
		str.Format(_T("db_get_count_of_folder = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO42:
		res = db_get_folder_records(m_InputString.GetBuffer(), m_InputOffset, folder_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_folder_records error\r\n\r\n"));
		else
			ShowFolderRecords(folder_rec, res);
		break;

	case IDC_RADIO43:
		res = db_build_fpl(m_InputString.GetBuffer());
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_build_fpl error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_build_fpl OK\r\n\r\n"));
		break;

	case IDC_RADIO44:
		res = db_get_count_of_fpl(m_InputString.GetBuffer());
		str.Format(_T("db_get_count_of_fpl = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO45:
		res = db_get_fpl_records(m_InputString.GetBuffer(), m_InputOffset, folder_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_fpl_records error\r\n\r\n"));
		else
			ShowFolderRecords(folder_rec, res);
		break;

	case IDC_RADIO46:
		res = db_get_fpl_name(m_InputString.GetBuffer(), m_InputOffset, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_fpl_name error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO47:
		res = db_get_fpl_index(m_InputString.GetBuffer());
		str.Format(_T("db_get_fpl_index = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO48:
		res = db_delete_file(m_InputString.GetBuffer());
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_delete_file error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_delete_file OK\r\n\r\n"));
		break;

	case IDC_RADIO49:
		res = db_get_audio_id3(m_InputString.GetBuffer(), audio_rec);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_audio_id3 error\r\n\r\n"));
		else
			ShowAudioRecords(audio_rec, 1);
		break;

	case IDC_RADIO50:
		res = db_like_build();
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_like_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_like_build OK\r\n\r\n"));
		break;

	case IDC_RADIO51:
		res = db_get_like_count(m_InputOffset);
		str.Format(_T("db_get_like_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO52:
		res = db_get_like_records(m_InputOffset, 0, video_rec, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_like_records error\r\n\r\n"));
		else
			ShowVideoRecords(video_rec, res);
		break;

	case IDC_RADIO53:
		res = db_get_like_name(m_InputOffset, 0, utf8name, 20);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_like_name error\r\n\r\n"));
		else
			ShowUtf8Name(utf8name, res);
		break;

	case IDC_RADIO54:
		{
			int like;
			res = db_is_like(m_InputString.GetBuffer(), &like);
			if (res==-1)
				SetDlgItemText(IDC_EDIT1, _T("db_is_like error\r\n\r\n"));
			else
			{
				str.Format(_T("db_is_like = %#08x\r\n"), like);
				SetDlgItemText(IDC_EDIT1, str);
			}
		}
		break;

	case IDC_RADIO55:
		res = db_set_like(m_InputString.GetBuffer(), m_InputOffset);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_set_like error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_set_like OK\r\n\r\n"));
		break;

	case IDC_RADIO56:
		res = db_tag_build();
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_tag_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_tag_build OK\r\n\r\n"));
		break;

	case IDC_RADIO57:
		res = db_get_tag_count();
		str.Format(_T("db_get_tag_count = %d\r\n"), res);
		SetDlgItemText(IDC_EDIT1, str);
		break;

	case IDC_RADIO58:
		res = db_get_tag_record(m_InputString.GetBuffer(), tag_rec);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_get_tag_record error\r\n\r\n"));
		else
			ShowTagRecord(tag_rec[0], res);
		break;

	case IDC_RADIO59:
		res = db_add_tag(m_InputString.GetBuffer(), m_InputOffset, 0x1234);
		if (res==-1)
			SetDlgItemText(IDC_EDIT1, _T("db_add_tag error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_add_tag OK\r\n\r\n"));
		break;

	case IDC_RADIO60 :
		res = db_album_build();
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_album_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_album_build OK\r\n\r\n"));
		break;

	case IDC_RADIO61 :
		res = db_artist_build();
		if (res)
			SetDlgItemText(IDC_EDIT1, _T("db_artist_build error\r\n\r\n"));
		else
			SetDlgItemText(IDC_EDIT1, _T("db_artist_build OK\r\n\r\n"));
		break;

	default:
		ASSERT(0);	//not arrived
		break;
	}
}

void CdbDlg::ShowAudioRecords(AUDIORECORD records[20], int nTotal)
{
	CString str;
	CString tmp;
	CString path;
	CString name;
	CString album;
	CString artist;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for(int i=0; i<nTotal && i<20; i++)
	{
#ifdef	UNICODE
		path = Utf8ToWideChar(records[i].path);
#else
		path = records[i].path;
#endif
		name = Utf8ToWideChar(records[i].fname);
		album = Utf8ToWideChar(records[i].album);
		artist = Utf8ToWideChar(records[i].artist);

		//tmpA.Format("%s\r\n", records[i].path);
		tmp.Format(_T("%s\r\n"), path.GetBuffer());
		str += tmp;

		tmp.Format(_T("name=%s\r\n"), name.GetBuffer());
		str += tmp;

		tmp.Format(_T("album=%s\r\n"), album.GetBuffer());
		str += tmp;

		tmp.Format(_T("artist=%s\r\n"), artist.GetBuffer());
		str += tmp;

		str += _T("\r\n");
	}

	SetDlgItemText(IDC_EDIT1, str);
}

void CdbDlg::ShowVideoRecords(VIDEORECORD records[20], int nTotal)
{
	CString str;
	CString tmp;
	CString path;
	CString name;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for(int i=0; i<nTotal && i<20; i++)
	{
#ifdef	UNICODE
		path = Utf8ToWideChar(records[i].path);
#else
		path = records[i].path;
#endif
		name = Utf8ToWideChar(records[i].fname);

		//tmpA.Format("%s\r\n", records[i].path);
		tmp.Format(_T("%s\r\n"), path.GetBuffer());
		str += tmp;

		tmp.Format(_T("name=%s\r\n"), name.GetBuffer());
		str += tmp;

		str += _T("\r\n");
	}

	SetDlgItemText(IDC_EDIT1, str);
}

void CdbDlg::ShowEBookRecords(EBOOKRECORD records[20], int nTotal)
{
	CString str;
	CString tmp;
	CString path;
	CString name;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for(int i=0; i<nTotal && i<20; i++)
	{
#ifdef	UNICODE
		path = Utf8ToWideChar(records[i].path);
#else
		path = records[i].path;
#endif
		name = Utf8ToWideChar(records[i].fname);

		//tmpA.Format("%s\r\n", records[i].path);
		tmp.Format(_T("%s\r\n"), path.GetBuffer());
		str += tmp;

		tmp.Format(_T("name=%s\r\n"), name.GetBuffer());
		str += tmp;

		tmp.Format(_T("ebookSN=%d\r\n"), records[i].ebookSN);
		str += tmp;

		str += _T("\r\n");
	}

	SetDlgItemText(IDC_EDIT1, str);
}

void CdbDlg::ShowFolderRecords(FOLDERRECORD records[20], int nTotal)
{
	CString str;
	CString tmp;
	CString path;
	CString name;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for(int i=0; i<nTotal && i<20; i++)
	{
#ifdef	UNICODE
		path = Utf8ToWideChar(records[i].path);
#else
		path = records[i].path;
#endif
		name = Utf8ToWideChar(records[i].fname);

		//tmpA.Format("%s\r\n", records[i].path);
		tmp.Format(_T("%s\r\n"), path.GetBuffer());
		str += tmp;

		tmp.Format(_T("name=%s\r\n"), name.GetBuffer());
		str += tmp;

		tmp.Format(_T("media_type=%d\r\n"), records[i].media_type);
		str += tmp;

		str += _T("\r\n");
	}

	SetDlgItemText(IDC_EDIT1, str);
}

void CdbDlg::ShowTagRecord(TAGRECORD records, int nTotal)
{
	CString str;
	CString tmp;
	CString path;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for (int i = 0; i < nTotal && i < 20; i++)
	{
#ifdef	UNICODE
		path = Utf8ToWideChar(records[i].path);
#else
		path = records.path;
#endif

		tmp.Format(_T("%s\r\n"), path.GetBuffer());
		str += tmp;

		for (int j = 0; j < MAXTAG; j++)
		{
			tmp.Format(_T("tag[%d]=%d\r\n"), j, records.tag[j]);
			str += tmp;
			str += _T("\r\n");
		}
	}

	SetDlgItemText(IDC_EDIT1, str);
}

void CdbDlg::ShowUtf8Name(RECUTF8NAME items[20], int nTotal)
{
	CString str;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for(int i=0; i<nTotal && i<20; i++)
	{
		str += Utf8ToWideChar(items[i]);
		str += _T("\r\n");
	}

	SetDlgItemText(IDC_EDIT1, str);
}

void CdbDlg::ShowID3Item(RECID3ITEM items[20], int nTotal)
{
	CString str;

	str.Format(_T("nTotal = %d\r\n\r\n"), nTotal);

	for(int i=0; i<nTotal && i<20; i++)
	{
		str += Utf8ToWideChar(items[i]);
		str += _T("\r\n");
	}

	SetDlgItemText(IDC_EDIT1, str);
}

CString CdbDlg::Utf8ToWideChar(char * txt)
{
	wchar_t widestr[MAXPATH];
	MultiByteToWideChar(CP_UTF8, 0, txt, -1, widestr, MAXPATH);
	return CString(widestr);
}

CString CdbDlg::WideCharToUtf8(CString str)
{
	char utf8str[MAXPATH] = {0};
	wchar_t widestr[MAXPATH] = {0};
#ifdef UNICODE
	wsprintf(widestr, str);
#else
	MultiByteToWideChar(CP_ACP, 0, str, -1, widestr, MAXPATH);
#endif
	WideCharToMultiByte(CP_UTF8, 0, widestr, -1, utf8str, MAXPATH, NULL, FALSE);
	return CString(utf8str);
}
